# -*- coding: utf-8 -*-
# @Time : 2018/12/25 9:58
# @Author : Administrator
# @File : httptestV4.py
# @Project : python_demo
# @title : requests库-接口测试
import json

import requests
class Http_Requests():
    def test_general(self):#通用方法
        res_get=requests.request(method="GET",url="http://localhost:8088/agileone/")
        print(res_get.text)
        print(res_get.content)
        print(res_get.encoding)
        print(res_get.cookies)
        print(res_get.headers)
        print(res_get.url)
        print(res_get.status_code)

        res_post=requests.request(method="POST",url="http://localhost:8088/agileone/index.php/common/login",
                                  data={"username":"admin","password":"admin","savelogin":"true"})
        print(res_post.text)
    #
    def test_get(self):#GET请求
        # res=requests.get(url="http://localhost:8088/smeoa/index.php",params={"m":"login","a":"index"})
        # print(res.text)
        res = requests.get(url="http://localhost:8088/smeoa/index.php?m=login&a=index")
        print(res.text)

    def test_post(self):#POST请求
        data_login ={"username":"admin","password":"admin","savelogin":"false"}

        res=requests.post(url="http://localhost:8088/agileone/index.php/common/login",
                      data=data_login)
        # print(res.text)
        # print(res.headers)
        print(res.headers.get(key="Set-Cookie"))

    def test_other(self):
        r_put = requests.put("http://httpbin.org/put")
        print(r_put.text)
        res=r_put.text

        json_res=json.loads(res)
        print(json_res)

        r_delete = requests.delete("http://httpbin.org/delete")
        print(r_delete.text)

        r_head = requests.head("http://httpbin.org/get")
        print(r_head.text)

        r_options = requests.options("http://httpbin.org/get")
        print(r_options.text)

    def test_session(self):#创建session，利用session自动管理cookie
        ses=requests.session()#实例化一个session
        ses.post(url="http://localhost:8088/agileone/index.php/common/login",
                 data={"username":"admin","password":"admin","savelogin":"false"})
        res=ses.post(url="http://localhost:8088/agileone/index.php/notice/add",
                 data={"headline":"sda","content":"asd","scope":"1","expireddate":"2019-03-25"})
        print(res.text)


    def test_upload02(self):#上传英文文件名的文件
        ses = requests.session()  # 实例化一个session
        ses.post(url="http://localhost:8088/agileone/index.php/common/login",
                 data={"username": "admin", "password": "admin", "savelogin": "false"})

        file_path="E:\\FTP\\test001.png"#上传的文件绝对路径

        filename={"fileToUpload":open(file_path,mode="rb")}#指定需要上传的文件

        res=ses.post(url="http://localhost:8088/agileone/index.php/attach/upload/refertype/defect/referid/4",
                 files=filename)
        print(res.text)

    def test_upload03(self):#上传文件名含有中文的文件
        ses = requests.session()  # 实例化一个session
        ses.post(url="http://localhost:8088/agileone/index.php/common/login",
                 data={"username": "admin", "password": "admin", "savelogin": "false"})



        file_path="E:\\FTP\\sales-20171020-Test.xls"#上传的文件绝对路径

        filename={"fileToUpload":("test001.png",(open(file_path,mode="rb")))}#指定需要上传的文件



        res=ses.post(url="http://localhost:8088/agileone/index.php/attach/upload/refertype/defect/referid/4",

                 files=filename)
        print(res.text)

    def test_load(self):#下载文件
        res=requests.get(url="http://www.woniuxy.com/page/img/banner/allopen-home.jpg")
        # print(res.content)
        with open("E:\FTP\woniu.png",mode="wb") as f:
            f.write(res.content)
    def test_upload(self):
        ses = requests.session()  # 实例化一个session
        ses.post(url="http://localhost:8088/agileone/index.php/common/login",
                 data={"username": "admin", "password": "admin", "savelogin": "false"})
        res=ses.post(url="http://localhost:8088/agileone/index.php/attach/upload/refertype/defect/referid/4",

                 files={"fileToUpload":open(r"E:\FTP\bugreport.log",mode="rb")})
        print(res.text)

    def test_upload001(self):
        ses = requests.session()  # 实例化一个session
        ses.post(url="http://localhost:8088/agileone/index.php/common/login",
                 data={"username": "admin", "password": "admin", "savelogin": "false"})
        res = ses.post(url="http://localhost:8088/agileone/index.php/attach/upload/refertype/defect/referid/4",

                       files={"fileToUpload": ("report.log",open(r"E:\FTP\bugreport - 副本.log", mode="rb"))})
        print(res.text)

if __name__ == '__main__':
    Http_Requests().test_upload001()
